﻿namespace SingletonPattern
{
    /// <summary>
    /// 传统线程安全的写法
    /// </summary>
    internal sealed class Singleton
    {
        private volatile static Singleton _instance = null;
        private static readonly object lockHelper = new object();
        private Singleton() { }
        public static Singleton CreateInstance()
        {
            if (_instance == null)
            {
                lock (lockHelper)
                {
                    if (_instance == null)
                        _instance = new Singleton();
                }
            }
            return _instance;
        }

        public void TestMsg()
        {
            System.Console.WriteLine("传统写法");
        }
    }

    /// <summary>
    /// .net特有写法
    /// </summary>
    internal sealed class SingletonB
    {
        private SingletonB() { }
        public static readonly SingletonB Instance = new SingletonB();
        public void TestMsg()
        {
            System.Console.WriteLine(".net特有写法");
        }
    }


}

